package com.lemon.smartsafety.ui.adapter.fire;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.result.fire.BuildingTypeScore;
import com.lemon.smartsafety.ui.activity.fire.FirefightingUtil;
import com.lemon.smartsafety.utils.SystemUtil;
import com.lemon.smartsafety.view.numberprogressbar.NumberProgressBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/11 11:10
 * 修改人：Fargo
 * 修改时间：2016/8/11 11:10
 * 修改备注：
 */
public class NumberProgressBarAdapter extends BaseAdapter {

    /**
     * TAG
     */
    private final String TAG = "NumberProgressBarAdapter";

    /**
     * 选择项
     */
    private int m_nSelectItem = -1;

    /**
     * 数据列表
     */
    private List<BuildingTypeScore> mList;


    /**
     * 布局泵
     */
    private LayoutInflater m_layInflater = null;

    private Context mContext;

    private float mWidth=0;

    //百分比差值
    private float mSize=0;
    private float m1dp=0;



    /**
     * 设置选择项
     *
     * @param n
     */
    public void setSelectItem(final int n) {
        m_nSelectItem = n;
    }

    /**
     * 设置数据列表
     */
    public void setList(ArrayList<BuildingTypeScore> list) {
        this.mList = list;
    }

    /**
     * 构造函数
     *
     * @param context
     */
    public NumberProgressBarAdapter(Context context, List<BuildingTypeScore> list) {
        try {
            this.mContext = context;

            this.mList = list;

            // 布局泵
            m_layInflater = LayoutInflater.from(context);

        } catch (Exception e) {
        }
    }

    @Override
    public int getCount() {
        try {

           if (mList != null)
                return mList.size();
        } catch (Exception e) {
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null)
                return mList.get(position);
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {

            final ViewHolder holder;
            if (convertView == null) {
                convertView = m_layInflater.inflate(R.layout.item_progress_statistics, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if(mSize==0){
                m1dp=mContext.getResources().getDimension(R.dimen.dp_1);
                mWidth= SystemUtil.getWindowWidth(mContext);
                mSize=(mWidth*7/10-30*m1dp)/100;
            }


            if(position==(mList.size()-1)){
                holder.lineBtm.setVisibility(View.VISIBLE);
                holder.txtLineHor.setVisibility(View.VISIBLE);
            }else{
                holder.lineBtm.setVisibility(View.GONE);
                holder.txtLineHor.setVisibility(View.GONE);
            }


            holder.txtTitle.setText(mList.get(position).getTypeName());
            holder.numberbar.setReachedBarColor(ContextCompat.getColor(mContext, FirefightingUtil.getScoreTextColor(mList.get(position).getStatus())));
            holder.numberbar.setProgress(mList.get(position).getScore());

            holder.txtNum.setText(String.valueOf(mList.get(position).getScore()));
            holder.txtNum.setTextColor(ContextCompat.getColor(mContext, FirefightingUtil.getScoreTextColor(mList.get(position).getStatus())));
            holder.txtNum.setPadding((int) ((mList.get(position).getScore()+1)*mSize),0,0,0);

            holder.txtTitle.setSelected(true);

         /*   if(position%4==0){
                holder.txtTitle.setText("商户");
                holder.numberbar.setReachedBarColor(ContextCompat.getColor(mContext, R.color.colorAppYellow));
                holder.numberbar.setProgress(51);

                holder.txtNum.setText("51");
                holder.txtNum.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppYellow));
                holder.txtNum.setPadding((int) (52*mSize),0,0,0);
            }else if(position%4==2){
                holder.txtTitle.setText("旅游景区");
                holder.numberbar.setReachedBarColor(ContextCompat.getColor(mContext, R.color.colorAppGreen));
                holder.numberbar.setProgress(100);

                holder.txtNum.setText("100");
                holder.txtNum.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppGreen));
                holder.txtNum.setPadding((int) (101*mSize),0,0,0);
            }else if(position%4==3){
                holder.txtTitle.setText("食品生产类");
                holder.numberbar.setReachedBarColor(ContextCompat.getColor(mContext, R.color.colorAppRed));
                holder.numberbar.setProgress(34);

                holder.txtNum.setText("34");
                holder.txtNum.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppRed));
                holder.txtNum.setPadding((int) (35*mSize),0,0,0);
            }
            else if(position%4==1){
                holder.txtTitle.setText("石油石化企业");
                holder.numberbar.setReachedBarColor(ContextCompat.getColor(mContext, R.color.colorAppGreen));
                holder.numberbar.setProgress(74);

                holder.txtNum.setText("74");
                holder.txtNum.setTextColor(ContextCompat.getColor(mContext, R.color.colorAppGreen));
                holder.txtNum.setPadding((int) (75*mSize),0,0,0);
            }*/




        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }



    static class ViewHolder {
        @Bind(R.id.txt_title)
        TextView txtTitle;
        @Bind(R.id.txt_line_hor)
        TextView txtLineHor;
        @Bind(R.id.numberbar)
        NumberProgressBar numberbar;
        @Bind(R.id.txt_Num)
        TextView txtNum;
        @Bind(R.id.lineBtm)
        LinearLayout lineBtm;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    /**
     * 项支持器类
     */


}